home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 April / EnigmA AMIGA RUN 06 (1996)(G.R. Edizioni)(IT)[!][issue 1996-04][Skylink CD V].iso / internet / others / npns.lha / NPNS / postnews.c < prev    next >
C/C++ Source or Header  |  1995-09-30  |  7KB  |  307 lines

  1.  
  2. /*
  3.  *    Program     postnews
  4.  *    Programmer    N.d'Alterio
  5.  *    Date        06/07/95
  6.  *
  7.  *  Synopsis:    This program spools an news article ready for posting at
  8.  *              some later time. If currently online then sendnews is 
  9.  *              called and the article will be sent off immediately.
  10.  *        The online check is done by trying to open TCP: daytime
  11.  *              port.
  12.  *
  13.  *        This is a heavily modified version of postnews by 
  14.  *              James Burton from his postnewsspool package. This fixes
  15.  *              a number of bugs such as the leaving of locks, hanging
  16.  *              for a long time while waiting to see if it is online.
  17.  *        In addition the program has been very much streamlined
  18.  *              there are no longer the command line options -X, -R as
  19.  *              these seemd to have no use. Also gone are the arg### and
  20.  *              ref### files it produced which seemed to be useless.
  21.  *    
  22.  *        For tin users the program has the command line option
  23.  *              -from the add a From: user@somewhere to the header.
  24.  *
  25.  *        This program has the paths for my setup compiled into
  26.  *              it as defaults to use different paths and files there
  27.  *              is an env variable PNS_SPOOLDIR which is the full directory
  28.  *              name for the spool dir with no trailing '/'.
  29.  *
  30.  *              The program expects to have the companion program sendnews
  31.  *              in AmiTCP:bin/sendnews which in turn needs to have the 
  32.  *              NNTP posting program as AmiTCP:bin/NNTPPost.     
  33.  *
  34.  *  Command Line Arguments:    -from        Adds a From: someone@ line to header
  35.  *                              -h              Prints some help info
  36.  *                filename    The article file to post
  37.  *                                              if omitted will take from STDIN
  38.  *
  39.  *  Inputs:        article            Should contain a full header
  40.  *
  41.  *  Outputs:        spooldir/msg.###     The spooled message
  42.  *
  43.  *  Variables:        int i            general loop var
  44.  *            int DoStdin        flag 
  45.  *            int exit_code        return code for prog
  46.  *            BPTR lock        lock on spooldir
  47.  *            char *Buffer         string to build filenames
  48.  *            FILE *msgfile        file to contain spooled  article
  49.  *            int no_post        flag - don't try to send
  50.  *
  51.  *  Functions:        copyfile        copies article to spooldir
  52.  *
  53.  * $VER: postnews.c 2.1 (07.08.95) $        
  54.  * $Log: postnews.c $
  55.  * Revision 3.3  1995/09/30  21:25:18  daltern
  56.  * new command line option -n
  57.  *
  58.  * Revision 3.2  1995/09/30  21:17:20  daltern
  59.  * Added new env var PNS_OFFLINE
  60.  * improved error handling
  61.  * ,
  62.  *
  63.  * Revision 3.1  1995/09/10  16:41:41  daltern
  64.  * Removed need for seq file to comply with v3.1 of sendnews
  65.  * spooled messages now have name of form msg.##########
  66.  * where ###### is the long date returned by time()
  67.  *
  68.  *
  69.  * Revision 1.2  1995/08/27  21:49:10  daltern
  70.  * Added env vars for path options
  71.  * fixed a bug where a file was left open
  72.  *
  73.  * Revision 1.1  1995/08/07  14:17:55  daltern
  74.  * Initial revision
  75.  *
  76.  *
  77.  */
  78.  
  79.  
  80. #include <stdio.h>                   
  81. #include <stdlib.h>
  82. #include <string.h>
  83. #include <time.h>
  84. #include <dos/dos.h>
  85. #include <clib/dos_protos.h>
  86. #include <clib/exec_protos.h>
  87. #include <exec/types.h>
  88.  
  89. #define SPOOLDIR    "AmiTCP:Usr/Spool/News"
  90.  
  91. int  copyfile( char *, FILE * );
  92.  
  93. extern char *version = "$VER: postnews 3.3 (30.09.95) Nick d'Alterio";
  94. extern struct Library *DOSBase;
  95.  
  96. int main( int argc,char **argv )
  97.  
  98. {
  99.  
  100.   FILE *msgfile;
  101.     
  102.   int i;          
  103.   int DoStdin   = TRUE;
  104.   int exit_code = 0;
  105.   int no_post   = FALSE;
  106.  
  107.   char *spooldir;
  108.  
  109.   char Buffer[100];   
  110.  
  111.   BPTR lock;                     
  112.  
  113.   FILE *fptr;
  114.  
  115.  
  116. /*
  117.  *   Read spool dir from env variable.
  118.  */
  119.  
  120.   if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {
  121.     spooldir = SPOOLDIR;
  122.   }   /* end if */
  123.  
  124. /*
  125.  *   See if user wants to post or not if online.
  126.  */
  127.  
  128.   if ( getenv( "PNS_OFFLINE" ) ) {
  129.     no_post = TRUE;
  130.   }   /* end if */
  131.  
  132. /*
  133.  *   Open dos library > v37
  134.  */
  135.  
  136.   if ( OpenLibrary( "dos.library", 37 ) ) {
  137.  
  138. /*
  139.  *   Lock directory while using.
  140.  */
  141.       
  142.     if ( lock = Lock( spooldir, ACCESS_READ ) ) {
  143.  
  144.  
  145. /*
  146.  *   Open file to spool articles to.
  147.  */
  148.  
  149.           sprintf( Buffer, "%s/msg.%ld", spooldir, time( NULL ) );
  150.           if ( msgfile = fopen( Buffer, "w" ) ) {
  151.  
  152. /*
  153.  *   Parse command line.
  154.  */
  155.   
  156.             i = 0;
  157.               while ( i < argc ) {    
  158.  
  159.                 i++;
  160.                 if ( argv[i][0] == '-' ) {   
  161.  
  162.                     switch ( argv[i][1] ) {
  163.        
  164.                                 case 'f' : 
  165.  
  166.                             if ( strcmp( argv[i], "-from" ) == 0 ) {
  167.  
  168.                                 fprintf( msgfile, "From: %s\n", argv[i+1] );
  169.  
  170.                             }   /* end if */
  171.             
  172.                             i++;
  173.                             break;
  174.  
  175.                         case 'h' :
  176.  
  177.                             fprintf( stderr, " Usage: postnews [-from me@somewhere][-n] article_filename\n" );
  178.                                         fprintf( stderr, "\n Article is taken from stdin if no filename is given\n" );
  179.                                        break;
  180.  
  181.                         case 'n' :
  182.  
  183.                             no_post = TRUE;
  184.                             break;
  185.  
  186.                         default :
  187.  
  188.                             fprintf( stderr, " Unknown option use -h for help\n" );
  189.                             break;
  190.  
  191.                     }   /* end switch */
  192.  
  193.                     } else {   
  194.  
  195. /*
  196.  *   Move article file to spool file.
  197.  */
  198.  
  199.                     if ( argv[i] != NULL && argv[i][0] != '\0' ) {
  200.  
  201.                         exit_code = copyfile( argv[i], msgfile );
  202.                         DoStdin = FALSE;
  203.             
  204.                     }   /* end if */
  205.  
  206.                 }   /* end if */
  207.  
  208.               }   /* end while */                    
  209.     
  210. /*
  211.  *   Move article from stdin to spool file.
  212.  */
  213.  
  214.             if ( DoStdin == TRUE ) {
  215.  
  216.                     int ch;
  217.                     while ( ( ch = getchar() ) != EOF ) fputc( ch, msgfile );
  218.     
  219.               }   /* end if */
  220.                   
  221.               fclose( msgfile );
  222.  
  223.         } else {
  224.  
  225.             fprintf( stderr, " Could not open message file - %s\n", Buffer );
  226.             exit_code = 10;
  227.  
  228.         }   /* end if open msg file */
  229.  
  230.           UnLock( lock );
  231.  
  232.     } else {
  233.  
  234.         fprintf( stderr, " Could not lock spooldir - %s\n", spooldir );
  235.         exit_code = 20;
  236.  
  237.     }   /* end if lock */
  238.  
  239.   } else {
  240.  
  241.     fprintf( stderr, " Could not open dos.library > v37\n" );
  242.     exit_code = 20;
  243.  
  244.   }   /* end if */         
  245.   
  246.     
  247. /*
  248.  *   Check if online. If yes then call sendnews to send off articles
  249.  *   now.
  250.  */
  251.  
  252.   if ( !exit_code ) {
  253.  
  254.     if ( ( ( fptr = fopen( "TCP:localhost/daytime", "r" ) ) == NULL ) || no_post ) {
  255.  
  256.         fprintf( stderr, " You are not online - message spooled\n" );
  257.  
  258.       } else {
  259.  
  260.         fclose( fptr );
  261.         fprintf( stderr, " Posting spooled news now....\n" );
  262.         system("amitcp:bin/sendnews");
  263.   
  264.       }   /* end if */
  265.  
  266.   }   /* end if !exit_code */
  267.  
  268.   exit(exit_code);
  269.  
  270. }   /* end program postnews */
  271.  
  272. /*========================================================================*
  273.                                   END
  274.  *========================================================================*/
  275.  
  276. /*========================================================================*
  277.                           BEGIN FUNCTION copyfile
  278.  *========================================================================*/
  279.  
  280. int copyfile( char *name, FILE *fp )
  281.  
  282.                
  283.   FILE *infile;
  284.  
  285.   int ch;
  286.     
  287.   infile = fopen( name, "r" );
  288.   if ( infile == NULL ) {
  289.  
  290.     fprintf( stderr, " Can't open article to spool\n" );
  291.     return 10;
  292.  
  293.   }   /* end if */
  294.  
  295.   while ( ( ch = fgetc( infile ) ) != EOF ) fputc( ch, fp );
  296.         
  297.   fclose( infile );
  298.  
  299.   return 0;
  300.  
  301. }   /* end function copyfile */
  302.  
  303. /*========================================================================*
  304.                               END copyfile
  305.  *========================================================================*/
  306.